package com.innovation.chengzhangriji.ui.widget;

import android.content.Context;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.innovation.chengzhangriji.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

public class MainToolbar extends Toolbar implements View.OnClickListener {

    public interface OnClickActionListener {
        void onClickActionMenu();

        void onClickNote();

        void onClickActionDiscover();
    }

    private OnClickActionListener mOnClickActionListener;

    private RoundedImageView mImageHead;

    private CuteTextView mTextName;

    private CuteTextView mTextDay;

    private DisplayImageOptions options; //配置图片加载及显示选项

    public MainToolbar(Context context) {
        super(context);
        init(context);
    }

    public MainToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MainToolbar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public void setOnClickActionListener(OnClickActionListener onClickActionListener) {
        mOnClickActionListener = onClickActionListener;
    }

    public void setBabyHead(String headUrl) {
        if (TextUtils.isEmpty(headUrl)) {
            mImageHead.setImageDrawable(getResources().getDrawable(R.drawable.ic_head_unknown));
        } else {
            ImageLoader.getInstance().displayImage(headUrl, mImageHead, options);
        }
    }

    public void setBabyName(String name) {
        mTextName.setText(name);
    }

    public void setBabyDay(String day) {
        mTextDay.setText(day);
    }

    private void init(Context context) {
        setContentInsetsAbsolute(0, 0);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.layout_main_toolbar, this);
        ImageView imageNote = (ImageView) findViewById(R.id.image_note);
        mImageHead = (RoundedImageView) findViewById(R.id.image_head);
        mTextName = (CuteTextView) findViewById(R.id.text_name);
        mTextDay = (CuteTextView) findViewById(R.id.text_date);
        CuteImageView actionMenu = (CuteImageView) findViewById(R.id.action_menu);
        CuteImageView actionFriend = (CuteImageView) findViewById(R.id.action_friend);

        imageNote.setOnClickListener(this);
        actionMenu.setOnClickListener(this);
        actionFriend.setOnClickListener(this);
        setOptions();
    }

    @Override
    public void onClick(View v) {
        if (mOnClickActionListener == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.action_menu:
                mOnClickActionListener.onClickActionMenu();
                break;
            case R.id.image_note:
                mOnClickActionListener.onClickNote();
                break;
            case R.id.action_friend:
                mOnClickActionListener.onClickActionDiscover();
        }
    }

    private void setOptions() {
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.ic_head_unknown)    //在ImageView加载过程中显示图片
                .showImageForEmptyUri(R.drawable.ic_head_unknown)  //image连接地址为空时
                .showImageOnFail(R.drawable.ic_head_unknown)  //image加载失败
                        // .cacheInMemory(true)  //加载图片时会在内存中加载缓存
                .cacheOnDisk(true)   //加载图片时会在磁盘中加载缓存
                .build();
    }
}
